package com.xian.jdbc;

import java.sql.*;

public class MysqlTest {
    public static void main(String[] args) throws Exception {

        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://r71/db1", "root", "123456");
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("show tables");
        while (rs.next()){
            String tablename = rs.getString(1);
            //String createSQL = rs.getString(2);
            System.out.println(tablename);
        }
        System.out.println("this geme is over !");

    }

    //获取建表语句
    public static void getCreateSQL(String dbname) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://r71/"+dbname, "root", "123456");
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("SHOW CREATE TABLE db2.t1");
        if (rs.first()){
            String tablename = rs.getString(1);
            String createSQL = rs.getString(2);
            System.out.println(createSQL);
        }
        System.out.println("this geme is over !");

    }



    //获取数据
    public static void getData() throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://r71/db2", "root", "123456");
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("select * from t1");
        while (rs.next()){
            int id = rs.getInt("id");
            String name = rs.getString("name");
            Date birthday = rs.getDate("birthday");
            Time birthday1 = rs.getTime("birthday");
            Timestamp birthday2 = rs.getTimestamp("birthday");
            System.out.println(id + "," + name + "," + birthday + "," + birthday1 + "," + birthday2);
        }
        System.out.println("this geme is over !");
    }
}
